package com.bizdk.generator.common.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 查询公共参数
 */
@Data
public class Query {
    
    String code;
    
    String tableName;
    
    String attrType;
    
    String columnType;
    
    String connName;
    
    String dbType;
    
    String projectName;
    
    @NotNull(message = "页码不能为空") @Min(value = 1, message = "页码最小值为 1") Integer pageNum;
    
    @NotNull(message = "每页条数不能为空") @Range(min = 1, max = 1000, message = "每页条数，取值范围 1-1000") Integer pageSize;
    
    String searchValue; // 搜索值
    
    String orderByColumn; // 排序字段
    
    String isAsc;
    
    Long datasrcId;
    
    Long projectId;

    public <T> QueryWrapper<T> getWrapper() {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(this.getCode()), "code", this.getCode());
        wrapper.like(StrUtil.isNotBlank(this.getTableName()), "table_name", this.getTableName());
        wrapper.like(StrUtil.isNotBlank(this.getAttrType()), "attr_type", this.getAttrType());
        wrapper.like(StrUtil.isNotBlank(this.getColumnType()), "column_type", this.getColumnType());
        wrapper.like(StrUtil.isNotBlank(this.getConnName()), "conn_name", this.getConnName());
        wrapper.eq(StrUtil.isNotBlank(this.getDbType()), "db_type", this.getDbType());
        wrapper.like(StrUtil.isNotBlank(this.getProjectName()), "name", this.getProjectName());
        wrapper.eq(null != this.getDatasrcId(), "datasrc_id", this.getDatasrcId());
        wrapper.eq(null != this.getProjectId(), "id", this.getProjectId());
        return wrapper;
    }
}
